package com.mango.module.ldap.service.impl;

import static org.springframework.ldap.query.LdapQueryBuilder.query;

import java.util.List;

import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.mango.module.ldap.domain.DepartDO;
import com.mango.module.ldap.domain.PersonDO;
import com.mango.module.ldap.mapper.DepartAttributesMapper;
import com.mango.module.ldap.mapper.PersonAttributesMapper;
import com.mango.module.ldap.service.IDepartService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("departService")
public class DepartServiceImpl implements IDepartService {

	@Autowired
	private LdapTemplate ldapTemplate;

	/**
	 * 查询部分字段集合
	 * 
	 * @return
	 */
	@Override
	public List<String> getAllDepartNames() {
		return ldapTemplate.search(query().where("objectclass").is("organizationalUnit"),
				(AttributesMapper<String>) attrs -> (String) attrs.get("ou").get());
	}

	@Override
	public List<DepartDO> getAll() {
		return ldapTemplate.search(query().where("objectclass").is("organizationalUnit"), new DepartAttributesMapper());
	}

	@Override
	public PersonDO findDepartWithDn(String dn) {
		return ldapTemplate.lookup(dn, new PersonAttributesMapper());
	}

	@Override
	public boolean addDO(DepartDO depart) {
		try {
			Attributes attrs = buildAttributes(depart);
			ldapTemplate.bind("ou="+depart.getCode()+",ou=people", null, attrs);
		} catch (Exception e) {
			log.error("添加部门异常", e);
			return false;
		}
		return true;
	}

	@Override
	public boolean updateDO(DepartDO depart) {
		try {
			ModificationItem[] mItems = new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
					new BasicAttribute("description", StringUtils.trimToEmpty(depart.getDescription()))) };
			ldapTemplate.modifyAttributes("ou=" + depart.getCode().trim(), mItems);
		} catch (Exception e) {
			log.error("更新部门异常", e);
			return false;
		}
		return true;
	}

	@Override
	public boolean delDO(String ou) {
		try {
			ldapTemplate.unbind("ou=" + ou.trim());
		} catch (Exception e) {
			log.error("删除部门异常", e);
			return false;
		}
		return true;
	}
	

	private Attributes buildAttributes(DepartDO depart) {
		// 基类设置
        Attributes attrs = new BasicAttributes();
        BasicAttribute ocAttr = new BasicAttribute("objectClass");
        ocAttr.add("top");
        ocAttr.add("organizationalUnit");
        // 属性设置
        attrs.put(ocAttr);
        attrs.put("ou", depart.getCode());/* 显示组id */
		attrs.put("description", StringUtils.trimToEmpty(depart.getDescription()));
        return attrs;
	}

	@Override
	public DepartDO getDepartByOu(String ou) {
		LdapQuery query = query().where("objectclass").is("organizationalUnit").and("ou").is(ou);
		List<DepartDO> list=ldapTemplate.search(query, new DepartAttributesMapper());
		return CollectionUtils.isEmpty(list)? null:list.get(0);
	}
}
